package mall.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import mall.bean.Product;
import mall.service.ProductService;
import mall.service.impl.ProductServiceImpl;

@WebServlet("/detail")
public class ProductDetailServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ProductService productService = new ProductServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 得到请求参数pid
		long pid = Long.parseLong(req.getParameter("pid"));
		// 调service去查
		Product product = this.productService.findById(pid);
		// 返回数据给前端
		Result result = product == null ? Result.error("无此商品") : Result.success(product);
		resp.setContentType("application/json;charset=UTF-8");
		resp.getWriter().append(JSON.toJSONString(result, true));
	}
}
